package net.gazhi.delonix.rbac.entity;

import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

/**
 * 菜单项<br>
 * 系统可以定制多套菜单，menuUsage 相同的菜单项属于同一菜单
 * 
 * @author Jeffrey Lin
 */
@Entity(name = DX_TABLE.dx_menu_item)
public class MenuItem implements Comparable<MenuItem> {

	@Id
	private int id;

	@Column(name = "icon_style")
	private String iconStyle;

	@Column
	private String name;

	@Column
	private String href;

	@Column(name = "target")
	private String target;

	@ManyToOne
	@JoinColumn(name = "parent_id")
	private MenuItem parent;

	@ManyToOne
	@JoinColumn(name = "action_id")
	private Action action;

	@Transient
	private Set<MenuItem> childItems = new LinkedHashSet<MenuItem>();

	public Set<MenuItem> getChildItems() {
		return childItems;
	}

	public MenuItem() {
	}

	public MenuItem(int id, String iconStyle, String name, String href, String target, Action action) {
		this.id = id;
		this.iconStyle = iconStyle;
		this.name = name;
		this.href = href;
		this.target = target;
		this.action = action;
	}

	@Override
	public int compareTo(MenuItem target) {
		return Integer.compare(this.id, target.id);
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getIconStyle() {
		return iconStyle;
	}

	public void setIconStyle(String iconStyle) {
		this.iconStyle = iconStyle;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getHref() {
		return href;
	}

	public void setHref(String href) {
		this.href = href;
	}

	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}

	public MenuItem getParent() {
		return parent;
	}

	public void setParent(MenuItem parent) {
		this.parent = parent;
	}

	public Action getAction() {
		return action;
	}

	public void setAction(Action action) {
		this.action = action;
	}

	public void setChildItems(Set<MenuItem> childItems) {
		this.childItems = childItems;
	}

	public boolean getIsRoot() {
		return this.getParent() == null;
	}

	public boolean getHasChild() {
		return this.getChildItems().size() > 0;
	}

}
